package com.example.flashsales.service.impl;

import com.example.flashsales.entity.dto.GoodsDto;
import com.example.flashsales.entity.dto.OrderCarDetailsDto;
import com.example.flashsales.entity.dto.OrderCarDto;
import com.example.flashsales.entity.pojo.OrderCar;
import com.example.flashsales.entity.pojo.OrderCarDetails;
import com.example.flashsales.entity.vo.GoodsVo;
import com.example.flashsales.entity.vo.OrderCarDetailsVo;
import com.example.flashsales.mappers.OrderCarMapper;
import com.example.flashsales.service.OrderCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderCarServiceImpl implements OrderCarService {

    @Autowired
    OrderCarMapper orderCarMapper;

    @Autowired
    GoodsServiceImpl goodsService;

    /**
     * 添加商品进入购物车(用户调用)
     *
     * @return 返回是否添加成功
     */
    @Transactional
    public String addGoodsInOrderCar(OrderCarDetailsDto orderCarDetailsDto, long userId) {
        final OrderCar car = getCarId(userId);
        if (car == null) {
            return "用户不存在";
        }
        final GoodsVo goodsVo = goodsService.viewGoodsMessage(orderCarDetailsDto.getGoodsId());
        if (goodsVo == null) {
            return "商品不存在";
        }
        //设置购物车id
        orderCarDetailsDto.setOrderCarId(car.getId());
        //设置细节价格
        double money = orderCarDetailsDto.getCount() * goodsVo.getPrice();
        orderCarDetailsDto.setMoney(money);
        //获取细节
        final OrderCarDetails orderCarDetails = orderCarMapper.viewGoods(orderCarDetailsDto.getGoodsId(), car.getId());
        if (orderCarDetails == null) {
            //添加细节
            if (orderCarMapper.addGoodsInOrderCar(orderCarDetailsDto) > 0) {
                //修改购物车的总价
                money = money + car.getMoney();
                if (orderCarMapper.updateOrderCarMoney(car.getId(), money) > 0) {
                    return "添加成功";
                }
            }
        }else {
            //如果存在则加数量改价钱
            return changeOrderCarGoodsCount(orderCarDetailsDto, userId);
        }

        return "添加失败";
    }


    /**
     * 修改用户购物车里面的商品信息 如果修改成0的话直接删除
     *
     * @param userId 用户id（用于查询购物车id）
     * @return 返回是否修改成功
     */
    @Transactional
    public String changeOrderCarGoodsCount(OrderCarDetailsDto orderCarDetailsDto, long userId) {
        final OrderCar car = getCarId(userId);
        if (car == null) {
            return "用户不存在";
        }
        orderCarDetailsDto.setOrderCarId(car.getId());
        //购物车订单信息
        final OrderCarDetails orderCarDetails = orderCarMapper.viewGoods(orderCarDetailsDto.getGoodsId(), orderCarDetailsDto.getOrderCarId());
        //商品信息
        final GoodsVo goodsVo = goodsService.viewGoodsMessage(orderCarDetailsDto.getGoodsId());
        if (orderCarDetails == null) {
            return "商品不存在";
        }
        //修改剩余数量
        double count = orderCarDetails.getCount() + orderCarDetailsDto.getCount();
        if (count < 0) {
            return "删除失败";
        }
        //减去的价格
        final double v = orderCarDetailsDto.getCount() * goodsVo.getPrice();
        double money = orderCarDetails.getMoney() + v;
        if (count == 0) {
            //直接删除数据
            return deleteGoodsInOrderCar(orderCarDetailsDto, userId);
        } else {
            //修改细节的数量与价格并修改总价格
            if (orderCarMapper.changeOrderCarGoodsCount(car.getId(), orderCarDetailsDto.getGoodsId(), count, money) > 0) {
                //修改总价格
                double m = car.getMoney() + v;
                if (orderCarMapper.updateOrderCarMoney(car.getId(), m) > 0) {
                    return "修改成功";
                }
            }
        }
        return "修改失败";
    }


    /**
     * 删除购物车里面的某条数据
     *
     * @param userId 用户id
     * @return
     */
    @Transactional
    public String deleteGoodsInOrderCar(OrderCarDetailsDto orderCarDetailsDto, long userId) {
        final OrderCar car = getCarId(userId);
        if (car == null) {
            return "用户不存在";
        }
        long id = car.getId();//用户的购物车id
        OrderCarDetails orderCarDetails = orderCarMapper.viewGoods(orderCarDetailsDto.getGoodsId(), id);
        if (orderCarMapper.deleteGoodsInOrderCar(id, orderCarDetailsDto.getGoodsId()) > 0) {
            //删除成功后修改金额
            //获取该商品的信息
            if (orderCarDetails == null) {

                return "删除失败,商品不存在";
            }
            double money = car.getMoney() - orderCarDetails.getMoney();
            if (orderCarMapper.updateOrderCarMoney(id, money) > 0) {
                return "删除成功";
            }
        }
        return "删除失败，商品不存在";
    }

    /**
     * 清空用户的购物车
     *
     * @param userId 用户id
     * @return 返回是否清空成功
     */
    @Override
    @Transactional
    public String cleanOrderCar(long userId) {
        final OrderCar carId = getCarId(userId);
        if (carId == null) {
            return "用户不存在";
        }
        if (orderCarMapper.cleanOrderCar(carId.getId()) > 0) {
            //修改购物车金额
            if (orderCarMapper.updateOrderCarMoney(carId.getId(), 0.0) > 0) {
                return "清空成功";
            }
        }
        return "清空失败";
    }


    /**
     * 根据用户id查询购物车id
     *
     * @param userId 用户id
     * @return 返回购物车
     */
    @Override
    public OrderCar getCarId(long userId) {
        return orderCarMapper.getCardId(userId);
    }


    /**
     * 用户查看购物车（先获取购物车id）
     *
     * @param userId 用户id
     * @return 返回购物车细节
     */
    @Override
    public List<OrderCarDetailsVo> viewUserOrderCarMessage(long userId) {
        final OrderCar car = getCarId(userId);
        if (car == null) {
            return null;
        }
        final List<OrderCarDetails> orderCarDetails = orderCarMapper.viewUserOrderCarMessage(car.getId());
        List<OrderCarDetailsVo> list = new ArrayList<>();
        for (OrderCarDetails orderCarDetail : orderCarDetails) {
            final GoodsVo goodsVo = goodsService.viewGoodsMessage(orderCarDetail.getGoodsId());
            OrderCarDetailsVo orderCarDetailsVo = new OrderCarDetailsVo();
            BeanUtils.copyProperties(orderCarDetail, orderCarDetailsVo);
            orderCarDetailsVo.setAllMoney(car.getMoney());
            orderCarDetailsVo.setGoodsName(goodsVo.getName());
            orderCarDetailsVo.setGoodsPrice(goodsVo.getPrice());
            list.add(orderCarDetailsVo);
        }
        return list;
    }


    /**
     * 增加一条纪录
     *
     * @param orderCarDto 购物车数据传输
     * @return 返回是否添加成功
     */
    @Override
    public long addOrderCar(OrderCarDto orderCarDto) {
        if (orderCarMapper.addOrderCar(orderCarDto) > 0) {
            return orderCarDto.getId();
        }
        return 0;
    }
}
